<template>
  <vxe-modal @close="cancel" :loading="loading" title="选择课题" v-model="dialogVisible" width="60%" showFooter esc-closable show-zoom
             resize>
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="75px">
      <el-form-item label="课题归属:" prop="ktGs">
        <el-select v-model="queryParams.ktGs" placeholder="请选择课题归属" clearable size="mini">
          <el-option
            v-for="dict in dict.type.kylx_ktgs"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年度:" prop="year">
        <el-select v-model="queryParams.year" placeholder="请选择年度" clearable size="mini">
          <el-option
            v-for="dict in dict.type.year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课题名称:" prop="ktName">
        <el-input
          size="mini"
          v-model="queryParams.ktName"
          placeholder="请输入课题名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table height="50vh" @row-click="clickRow" ref="table" highlight-current-row border
              :row-key="(row)=>{ return row.xmsbId}" :reserve-selection="true"
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="xmsbList" @selection-change="handleSelectionChange">
      <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
      <el-table-column label="年度" sortable align="center" prop="year">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.year"/>
        </template>
      </el-table-column>
      <el-table-column label="项目编号" sortable align="center" prop="ktBh"/>
      <el-table-column label="项目名称" sortable align="center" prop="ktName" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.ktName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="申报单位" sortable align="center" prop="zrBm" show-overflow-tooltip/>
      <el-table-column label="依托工程" align="center" prop="ytGc" show-overflow-tooltip/>
      <el-table-column label="项目总额(万)" sortable align="center" prop="xmze">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="xmZeButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.xmze }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="申请资助(万)" sortable align="center" prop="sqzz"/>
      <el-table-column label="课题归属" sortable align="center" prop="ktGs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kylx_ktgs" :value="scope.row.ktGs"/>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查询项目申报(主表 + 子表) 弹窗 -->
    <xmxq ref="xmxq"></xmxq>

    <!-- 查看项目预算 弹窗 -->
    <XmZe ref="xmze"></XmZe>

    <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
      <el-button icon="el-icon-check" size="mini" type="primary" @click="submitForm">保 存</el-button>
      <el-button icon="el-icon-close" size="mini" type="info" @click="cancel">关 闭</el-button>
    </div>
  </vxe-modal>
</template>

<script>
import {listXmsb} from "@/api/xmgl/lxgl/xmsb/xmsb";
import Xmxq from "@/views/xmgl/lxgl/xmsb/components/xmxq";
import XmZe from "@/views/xmgl/lxgl/xmsb/components/xmZe";

export default {
  name: "ktXz",
  components: {XmZe, Xmxq},
  dicts: ['kylx_fjlx', 'process_business_status', 'kylx_zcry_ktfg', 'year', 'kylx_ssly_cwxt', 'kylx_gqfw', 'kylx_country', 'sys_user_sex', 'kylx_xmfl_cwxt', 'kylx_ssly', 'kylx_hzfs', 'rcpt_zc', 'kylx_yfxmjb', 'kylx_ktgs', 'kylx_ktfl', 'kylx_bz'],
  data() {
    return {
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ktGs: null,
        year: null,
        ktName: null,
        deptId: null,
      },
      xmsbList: [],
      // 总条数
      total: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectedRow: null, // 保存当前选中的行
      ktXzList: [],
      yxKtXx: null, //已选择的课题
    }
  },
  inject: ['updateKtXzLabel'],
  methods: {
    cancel() {
      this.dialogVisible = false;
      this.$refs.table.clearSelection();
    },
    handleOpen(data) {
      // 接收来自父组件的数据
      this.yxKtXx = data
      this.dialogVisible = true;
      this.loading = true;
      listXmsb(this.queryParams).then(response => {
        this.xmsbList = response.rows;
        this.total = response.total;
        setTimeout(() => {
          this.loading = false;
        }, 400)

        // 默认选中已有的数据
        this.$nextTick(() => {
          this.yxKtXx.forEach(item => {
            const selectedRow = this.xmsbList.find(row => row.xmsbId == item.xmsbId)
            console.log('selectedRow:', selectedRow); // 打印 selectedRow 以进行调试
            if (selectedRow) {
              this.$refs.table.toggleRowSelection(selectedRow, true);
            }
          });
        });
      });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 查询项目申报列表
    getList() {
      this.loading = true;
      listXmsb(this.queryParams).then(response => {
        this.xmsbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log('从父组件中传来的已选择的课题信息', this.yxKtXx)
      this.ids = selection.map(item => item.xmsbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      // 提取 bz 和 createBy 属性
      this.ktXzList = selection.map(item => ({
        nd: item.year,        //年度
        ktBh: item.ktBh,      //项目编号
        ktName: item.ktName,  //项目名称
        ktGs: item.ktGs,      //课题归属
        zrBm: item.zrBm,      //申报单位
        xmze: item.xmze,      //项目总额
        qTime: item.qTime,    //开始时间
        zTime: item.zTime,    //结束时间
        xmsbId: item.xmsbId,  //项目申报ID
      }))
    },
    // 查询子表信息
    handleButtonClick(row) {
      this.$refs.xmxq.handleOpen(row);
    },
    // 查询项目总额信息(打开项目总额弹窗)
    xmZeButtonClick(row) {
      this.$refs.xmze.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      table.toggleRowSelection(row);
    },
    submitForm() {
      this.updateKtXzLabel(this.ktXzList);
      this.dialogVisible = false;
      this.$refs.table.clearSelection();
    },
  },
}
</script>

<style scoped>

</style>
